Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Data

Namespace sk.DAL
    Public MustInherit Class CategoryProvider
        Inherits DataAccess
        Sub New()
            Me.ConnectionString = Globals.Settings.Category.ConnectionString
        End Sub
        Private Shared _instance As CategoryProvider = Nothing
        Public Shared ReadOnly Property Instance() As CategoryProvider
            Get
                If IsNothing(_instance) Then
                    _instance = CType(Activator.CreateInstance(Type.GetType(Globals.Settings.Category.ProviderType)), CategoryProvider)
                End If

                Return _instance
            End Get
        End Property
        Public MustOverride Function GetAllCategories() As Generic.List(Of Category)
        Public MustOverride Function CreateCategory(ByVal Cat As Category) As Boolean
        Public MustOverride Function DeleteCategory(ByVal CatID As Integer) As Boolean
        Public MustOverride Function UpdateCategory(ByVal Cat As Category) As Boolean
        Public MustOverride Function GetCategory(ByVal CatID As Integer) As Category
        Public MustOverride Function GetNextID() As Integer

        Protected Overridable Function GetCategoryFromReader(ByVal reader As IDataReader) As Category
            'loads up a faculty member from the reader
            Dim cat As New Category
            With cat
                If IsDBNull(reader("CategoryID")) Then .CategoryID = 0 Else .CategoryID = CInt(reader("CategoryID"))
                If IsDBNull(reader("CategoryName")) Then .Name = "" Else .Name = reader("CategoryName").ToString
            End With
            Return cat
        End Function

        Protected Overridable Function GetCategoryCollectionFromReader( _
                ByVal reader As IDataReader) _
                As List(Of Category)
            Dim CategoryList As New List(Of Category)
            Try
                While (reader.Read)
                    CategoryList.Add(GetCategoryFromReader(reader))
                End While
            Catch ex As Exception
                Throw New Exception("from GetCategoryCollectionFromReader", ex)
            End Try
            Return CategoryList
        End Function

    End Class
End Namespace

